package mn.slide.labmed.database;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import mn.slide.labmed.Sortierung;
import mn.slide.labmed.model.Item;
import mn.slide.labmed.model.Model;
import android.content.Context;
import android.util.Log;

import com.j256.ormlite.android.apptools.OpenHelperManager;

public class ItemsStatics
{
	public static ArrayList<Item>	Items;
	public static ArrayList<Item>	newItemList;
	static DatabaseHelper			databaseHelper;
	static Context					context;

	public void initDb( Context context )
	{
		this.context = context;
		Items = new ArrayList<Item>();
		List<Model> arrayList;
		try
		{
			arrayList = getHelper().getModelDao().queryForAll();
			for (Model model : arrayList)
			{
				if (model.getZTITEL().length() == 0)
					model.setZTITEL(model.getZFELD1());
				Items.add(new Item(model.getZMODELID(), model.getZTITEL(), model.getZPARENT_ID(), model.getZSORTIERUNG(), model.getZFELD1()));
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
	}

	public static List<Model> fetchTodo( long modelId )
	{
		List<Model> result = null;
		try
		{
			result = getHelper().getModelDao().queryBuilder().where().eq("ZMODELID", modelId).query();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		return result;

	}
	public static Model getParentByDuplicateOrParent( Model model )
	{
		String duplicate = model.getZDUPLIKAT();
		if (duplicate.equals("-1"))
		{
			try
			{
				return getHelper().getModelDao().queryBuilder().where().eq("ZMODELID", model.getZPARENT_ID()).query().get(0);
			}
			catch (SQLException e)
			{
				e.printStackTrace();
				return null;
			}
		}
		else
		{
			try
			{
				return getHelper().getModelDao().queryBuilder().where().eq("ZMODELID", model.getZDUPLIKAT()).query().get(0);
			}
			catch (SQLException e)
			{
				e.printStackTrace();
				return null;
			}
		}
	}

	public static ArrayList<Item> fetchItemByKey( String key )
	{
		// System.gc();
		List<Model> result = null;
		Log.d("key", key);
		try
		{
			result = getHelper().getModelDao().queryBuilder().where().like("ZFELD1", "%" + key + "%").or().like("ZFELD2", "%" + key + "%").or().like("ZFELD3", "%" + key + "%").or().like("ZFELD4", "%" + key + "%").or().like("ZFELD5", "%" + key + "%").or().like("ZFELD6", "%" + key + "%").or().like("ZFELD7", "%" + key + "%").or().like("ZFELD8", "%" + key + "%").or().like("ZFELD2", "%" + key + "%").or().like("ZTITEL", "%" + key + "%").or().like("ZAKKREDITIERT", "%" + key + "%").or().like("ZINDIKATION", "%" + key + "%").or().like("ZINFO", "%" + key + "%").or().like("ZSYNONYM", "%" + key + "%").or().like("ZKOMMENTAR", "%" + key + "%").or().like("ZBEMERKUNG", "%" + key + "%").or().like("ZANSATZTAGE", "%" + key + "%").or().like("ZBEZUG", "%" + key + "%").query();
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		newItemList = new ArrayList<Item>();
		for (Model model : result)
		{
			if (model.getZTITEL().length() == 0)
				model.setZTITEL(model.getZFELD1());
			newItemList.add(new Item(model.getZMODELID(), model.getZTITEL(), model.getZPARENT_ID(), model.getZSORTIERUNG(), model.getZFELD1()));
		}
		return newItemList;
	}

	public static ArrayList<Item> fetchItemByParentId( String parentId )
	{
		ArrayList<Item> newItemList = new ArrayList<Item>();
		for (Item item : Items)
		{
			if (item.getParentId().equals(parentId))
				newItemList.add(item);
		}
		Collections.sort(newItemList, new Sortierung<Item>());
		return newItemList;
	}

	public static boolean withChild( String modelId )
	{
		for (Item item : Items)
		{
			if (item.getParentId().equals(modelId))
				return true;
		}
		return false;
	}

	public static DatabaseHelper getHelper()
	{
		if (databaseHelper == null)
			databaseHelper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
		return databaseHelper;
	}
}
